<template>
  <view class="main">
    <view class="search">
      <view class="icon"><uni-icons type="search" size="30"></uni-icons></view>
      <input type="text" placeholder="请输入您搜索的商品" v-model="goodsName" />
      <view class="searchButton" @click="search">搜索</view>
    </view>
    <view class="historyList">
      <view class="text">
        <view class="textSearch">最近搜索</view>
        <view class="icon" @click="deleteHistoryList"><uni-icons type="trash-filled" size="20"></uni-icons></view>
      </view>
      <view class="list">
        <view class="tab" @click="goList(item)" v-for="(item, index) in historyList" :key="index">{{ item }}</view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      goodsName: '',
      page: '',
      historyList: [],
    }
  },
  created() {
    const historyList = uni.getStorageSync('historyList') ? JSON.parse(uni.getStorageSync('historyList')) : []
    this.historyList = historyList
  },
  methods: {
    //更新历史
    updateHistoryList() {
      this.historyList.unshift(this.goodsName)
      const arr = [...new Set(this.historyList)]
      this.historyList = arr
      uni.setStorageSync('historyList', JSON.stringify(this.historyList))
    },
    //搜索
    async search() {
      if (this.goodsName === '') return
      uni.navigateTo({
        url: '/pages_sub1/product-list/product-list?goodsName=' + this.goodsName,
      })
      this.updateHistoryList()
    },
    // 点击历史标签跳转
    goList(item) {
      console.log(item)
      uni.navigateTo({
        url: '/pages_sub1/product-list/product-list?goodsName=' + item,
      })
      this.goodsName = item
      this.updateHistoryList()
    },
    //删除历史
    deleteHistoryList() {
      this.historyList = []
      uni.removeStorageSync('historyList')
    },
  },
}
</script>

<style lang="scss" scoped>
.main {
  background-color: #eee;
  padding: 20rpx;
  .search {
    position: relative;
    display: flex;
    justify-content: space-between;
    width: 100%;
    height: 100rpx;
    background-color: #fff;
    .icon {
      position: absolute;
      top: 20rpx;
      left: 20rpx;
    }
    input {
      height: 100rpx;
      padding-left: 100rpx;
      line-height: 100rpx;
      font-size: 40rpx;
    }
    .searchButton {
      width: 200rpx;
      height: 100rpx;
      color: #fff;
      font-size: 40rpx;
      line-height: 100rpx;
      text-align: center;
      background-color: red;
    }
  }
  .historyList {
    font-size: 36rpx;
    padding: 60rpx 0 40rpx 0;
    .text {
      display: flex;
      justify-content: space-between;
      color: #777;
      margin-bottom: 30rpx;
    }
    .list {
      display: flex;
      flex-wrap: wrap;
      justify-content: flex-start;
      .tab {
        width: 200rpx;
        height: 80rpx;
        color: #000;
        margin: 10rpx;
        background-color: #fff;
        border-radius: 40rpx;
        line-height: 80rpx;
        text-align: center;
        overflow: hidden;
      }
    }
  }
}
</style>
